package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.Card;
import com.cyz.common.bean.UserCard;
import com.cyz.common.mapper.UserCardMapper;
import com.cyz.user.userService.CardService;
import com.cyz.user.userService.UserCardService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.user.vo.CardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 34928
* @description 针对表【user_card】的数据库操作Service实现
* @createDate 2023-03-02 07:26:37
*/
@Service
public class UserCardServiceImpl extends ServiceImpl<UserCardMapper, UserCard>
    implements UserCardService {

    @Autowired
    private CardService cardService;

    @Override
    public List<CardVo> getCardsByUserId(Integer userId) {

        LambdaQueryWrapper<UserCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserCard::getUserId,userId);
        List<Card> cards = new ArrayList<>();
        list(lambdaQueryWrapper).stream()
                .map(UserCard::getCardId)
                .forEach(item -> {
                    cards.add(cardService.getById(item));
                });
        if(ObjectUtils.isEmpty(cards)){
            return null;
        }
        List<CardVo> cardVos = BeanCopyUtil.copyBeanList(cards, CardVo.class);
        return cardVos;
    }
}




